package com.slipper.system.icon.controller;

import com.google.gson.reflect.TypeToken;
import com.slipper.core.gson.GsonUtil;
import com.slipper.system.icon.bo.IconBO;
import com.slipper.system.icon.service.IconService;
import com.slipper.util.*;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/icon/")
public class IconController {
    private static final Logger logger = LoggerFactory.getLogger(IconController.class);

    @Resource
    private IconService iconService;

    @PostMapping(value = "/upload")
    public String upload(IconBO bo) throws IOException {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择文件！");
        }

        String filename = bo.getFile().getOriginalFilename();
        bo.setName(filename);
        if (Tool.isNotBlank(filename) && filename.toLowerCase().endsWith(".svg")) {
            bo.setType("svg");
            bo.setContent(new String(bo.getFile().getBytes(), StandardCharsets.UTF_8));
        } else {
            bo.setType("base64");
            bo.setContent(ImageUtil.imgToBase64(bo.getFile().getInputStream()));
        }
        if (bo.getContent().length() > 2 * 1024 * 1024) {
            return ResultUtil.failureResult("图标最大不能超过 2M！");
        }
        this.iconService.save(bo);
        bo.setFile(null);
        return ResultUtil.successResult(bo);
    }

    /**
     * 获取附件列表
     */
    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody IconBO bo) {
        return ResultUtil.successResult(this.iconService.queryList(bo));
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody IconBO bo) {
        return ResultUtil.deleteResult(iconService.delete(bo.getId()));
    }


    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        IconBO iconQO = new IconBO();
        iconQO.setBuiltin("0");
        List<IconBO> iconList = iconService.queryList(iconQO);
        String fileName = "icon-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(iconList)));
    }

    @PostMapping(value = "/importData")
    public String importData(IconBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }

        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            Type type = new TypeToken<List<IconBO>>(){}.getType();
            List<IconBO> iconList = GsonUtil.fromJson(AESUtil.decrypt(content), type);
            iconService.saveList(iconList);
            return ResultUtil.successResult("导入图标成功。");
        } catch (Exception e) {
            logger.error("文件解释出错！", e);
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
